const express = require("express");
const router = express.Router();
const Response = require("../utils/Response.js");
const fs = require("fs");

// 引入mysql连接池
const pool = require("../utils/db.js");

// 生成个人训练计划
router.get("/", (req, res, next) => {
    let obj = req.query;
    console.log(obj);
    pool.query("select * from plan", [], (err, result) => {
      if (err) {
        return next(err);
      }
      console.log(result);
      //结果是对象，如果对象下的属性length为0说明修改失败
      if (result.length === 0) {
        res.send({ code: 201, msg: "查询失败" });
      } else {
        res.send({ code: 200, msg: "查询成功", data: result });
      }
    });
  });

  // 每天对应不同的课程
router.get('/plan_course', (req, res, next) => {
	let obj = req.query;
	console.log(obj);
	pool.query(`
	select 
	 course_project.pid,
	 course_project.pic,
	 course_project.title,
	 course_project.duration,
	 course_project.week_id
	from 
		plan,course_project
	where 
		course_project.week_id=? && plan.uid=? && plan.id = course_project.week_id
	`, [obj.week_id,obj.uid], (err, result) => {
		if (err) {
			next(err);
			return;
		}
		console.log(result);
		if (result.length === 0) {
			res.send({ code: 201, msg: "失败" });
		} else {
			res.send({ code: 200, msg: "查询成功", data: result });
		}
	});
});




module.exports = router;
